package com.example.hw_calendar;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewDebug.FlagToString;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.hw_calendar_DB.CalendarData;
import com.example.hw_calendar_DB.CalendarManager;
import com.example.hw_calendar_DB.ToDoItemData;
import com.example.hw_calendar_DB.CalendarManager.NoComparableObjectException;

public class ListActivity extends Activity {

	public static final int REQUEST_CODE_ADDDATAACTIVITY = 1;
	
	ListView listView;
	Button btn;
	
	ListAdapter listAdapter;
	
	ArrayList<ToDoItemData> listList = new ArrayList<ToDoItemData>();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.list_layout);
	    
	    listList = CalendarManager.getInstance().getItemData();
	    
	    listView = (ListView)findViewById(R.id.listView1);
	    listAdapter = new ListAdapter(this, listList);
	    listView.setAdapter(listAdapter);
	    
	    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
	    	@Override
	    	public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
	    		// TODO Auto-generated method stub
	    		Intent i = new Intent(ListActivity.this, DetailActivity.class);
	    		i.putExtra(DetailActivity.PARAM_FIELD_ITEMDATA, position);
	    		i.setFlags(i.FLAG_ACTIVITY_NO_HISTORY);
	    		startActivity(i);
	    	}
		});
	    btn = (Button)findViewById(R.id.backCalendar);
	    btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Intent resultIntent = new Intent();
				setResult(RESULT_OK, resultIntent);
				finish();
			}
		});
	    
	    btn = (Button)findViewById(R.id.addSchedule);
	    btn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent i = new Intent(ListActivity.this, AddDataActivity.class);
				startActivityForResult(i, REQUEST_CODE_ADDDATAACTIVITY);
			}
		});
	    
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if(requestCode == REQUEST_CODE_ADDDATAACTIVITY){
    		if(resultCode == Activity.RESULT_OK){
    			Intent resultIntent = new Intent();
				setResult(RESULT_OK, resultIntent);
				finish();
    		}
    	}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
